home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 11 - 1995 / 11.02 Feb 95 / 11.02 Top 10 < prev    next >
Encoding:
Text File  |  1996-04-04  |  3.8 KB  |  77 lines  |  [TEXT/R*ch]

  1. The following Q&As have code you might find useful.
  2.  
  3. Q. I am having trouble using sizeof() with printf().  For example:
  4.  
  5. printf("char size is %d.", sizeof(char));
  6. outputs "char size is 0."  Why do I get the wrong result?
  7.  
  8. A. The output is wrong because the return value of the sizeof() function is a size_t (an unsigned long).  Use an %ld rather than a %d as a format specifier.  Thus, the correct syntax is
  9.      
  10.   "printf("The size of a char is %ld.", sizeof(char));" 
  11.  
  12. Q. How can I avoid problems deleting heap objects whose references are on the stack when using exception handling?  The pointer (which is on the stack) to the object becomes invalid when the stack unwinds and only the destructors for automatic objects are guaranteed to be called when an exception is thrown.
  13.  
  14. A. To handle this, declare pointers as volatile.  Last month we explained why you should use the volatile type.  Below is a practical example.  Use volatile file pointers so that stack unwinding does not reset the value of the pointer (prohibiting the file from being closed).
  15.  
  16.   funClass * volatile funClassPtr;    // Syntax for volatile declaration 
  17.   funClassPtr = NULL;         // Pointer to NULL guarantees delete as safe.
  18.   int myInt = 1;                      // Watch in debugger to see
  19.                                       //   the stack unwind.
  20.   try_ {
  21.     funClassPtr = TCL_NEW(funClass, ());//macro for new operator
  22.     myInt = 2;                          //put new value on stack
  23.     Failure (2, 100);                   // Force exception 
  24.   } 
  25.   catch_all_() {                      //catch block
  26.     
  27.   delete funClassPtr;                 //this calls destructor.
  28.      
  29.      // myInt is reset to 1, delete removes the object from the  heap 
  30.   }
  31.   end_try_                            //end of try block
  32.  
  33. Q. How can I use exception handling without using the Think Class Library?
  34.  
  35. A. To use exception handling without the Think Class Library, include BRLib and Exceptions.cp in your project.  Also, compile with the directive #define NO_TCL
  36. The four macros used to make exception handling work correctly are:
  37.      
  38.   AUTO_DESTRUCT_OBJECT
  39.   TCL_NEW
  40.   TCL_END_CONSTRUCTOR
  41.   TCL_START_DESTRUCTOR
  42.  
  43. The macro AUTO_DESTRUCT_OBJECT will guarantee that the destructor is called for an automatic object on the stack.  A destructor will only work on a completely constructed object.  TCL_END_CONSTRUCTOR helps the compiler to determine the complete construction of an object.
  44. Here’s an example that shows how the macros are used.
  45.  
  46. class funClass TCL_AUTO_DESTRUCT_OBJECT //macro in class header  
  47. public:
  48.     funClass()  {                       // no arg constructor 
  49.     cout<< "In constructor."<<endl;
  50.     char * myStr = new char[64];        // allocate memory 
  51.     TCL_END_CONSTRUCTOR                 // End of the constructor
  52.     }
  53.     virtual ~funClass() {               // virtual destructor 
  54.     TCL_START_DESTRUCTOR                // Beginning of the destructor 
  55.     cout<< "In destructor."<<endl;
  56.     delete [] myStr;                    // deallocate memory 
  57.     } 
  58. };
  59.            
  60. Q. I have a program that I’m converting from DOS and would like to be able to draw some simple graphics to the console window.  How can I do that?
  61.  
  62. A. Don’t do it.  If you draw to the console window, you will not receive update events.  However, if you just can’t help yourself from going down this path, here is how to do it.
  63.      
  64.  #include <iostream.h>
  65.     
  66.  WindowPtr myWindow;         // To be used for the console window.
  67.      
  68.  void main (void)    {    
  69.    cout << " ";                //A simple way to show the console.
  70.    myWindow = FrontWindow();   //Get a pointer to the console.
  71.    SetPort(myWindow);          //Set the port for drawing.
  72.    PenNormal();                //Set the pen for drawing.
  73.    LineTo(100,47);             //Draw a line.     
  74.  }
  75.  
  76.